package com.industry.framework.core.annountions.aspect;

import cn.dev33.satoken.stp.StpUtil;
import com.industry.framework.core.annountions.monitor.IdooMonitorController;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @Author nbZhuozh
 * @Date 2025-03-20  10:57
 **/
@Aspect
@Component
@Slf4j
public class CoreMonitorControllerAspect {
    private static final Logger logger = LogManager.getLogger("ServiceMonitorLogger");
    private static final String classPreFixStr = "【IDOO-Monitor-Controller】";

    @Around("@annotation(idooMonitorController)")
    public Object service(ProceedingJoinPoint joinPoint , IdooMonitorController idooMonitorController)
            throws Throwable {
        //获取注解配置参数
        String steptName = !StringUtils.isEmpty(idooMonitorController.value()) ? idooMonitorController.value() : "UN_KNOW";
        //获取方法参数
        Object[] args = joinPoint.getArgs();
        //方法名
        String sinatureName = joinPoint.getSignature().getName();
        //类全路径
        String className = joinPoint.getSignature().getDeclaringTypeName();
        //记录时间
        long startTime = System.currentTimeMillis();
        Object proceed = null;
        try {
            proceed = joinPoint.proceed();
        } catch (Exception e){
            log.error(classPreFixStr+ "步骤{}报错 ， 入参：{}，方法名称：{}，类路径：{}",steptName,args,sinatureName,className);
            throw e;
        } finally {
            //获取操作用户
            String infos = getUserName() == null ? String.valueOf(StpUtil.getSession().getLoginId()) : getUserName();
            //消耗时长
            long costTime = System.currentTimeMillis() - startTime;
            //构造结果
            monitorloggerBuilder(steptName,infos,costTime,proceed, Arrays.toString(args),idooMonitorController);
        }
        return proceed;
    }

    //具体应用实现
    public String getUserName(){
        return null;
    }

    //构造记录
    public void monitorloggerBuilder(String steptName, String infos, long costTime, Object proceed
            , String args , IdooMonitorController target){
        String loggerBuilder = classPreFixStr + steptName + "[执行完成]用户：" + infos ;
        if(target.logParams()){
            loggerBuilder += "[参数]：" + args;
        }
        if(target.logParams()){
            loggerBuilder += "[结果]：" + proceed;
        }
        if(target.hasAsync()){
            addLoggerAsync(loggerBuilder);
        }else{
            addLogger(loggerBuilder);
        }
    }


    public void addLogger(String template , Object... args){
        logger.info(template,args);
    }

    @Async
    public void addLoggerAsync(String template , Object... args){
        logger.info(template,args);
    }
}
